from django.http import HttpResponse

from public.message import query_currency_msg
from public.currency import Currency
from webpage.webClient import get_web_client
from webpage.mei.urls import register_get, register_post
from django.shortcuts import render


@register_get('inquiry')
def inquiry_get(request):
    result = ''
    if target := request.GET.get('target', ''):
        coin = get_web_client().send(query_currency_msg.build(
            user_id=int(target),
            currency=Currency.coin.name,
        ))
        result = f'金币：{coin}'
    return render(request, 'inquiry.html', {
        'target': target,
        'result': result,
    })


@register_post('inquiry')
def inquiry_post(request):
    if target := request.POST.get('target'):
        coin = get_web_client().send(query_currency_msg.build(
            user_id=int(target),
            currency=Currency.coin.name,
        ))
        return HttpResponse(coin)
    return HttpResponse('查询失败')
